import { onMounted, reactive } from 'vue'
import ApiLogSystem from '~/apis/log/ApiLogSystem'
import SText from '~/store/SText'

export const index = () => {
    const conf = reactive({
        table: {
            expandall: true,
            source: [] as any[],
            data: [] as any[],
            loading: true,
            page: {
                pageSize: 15,
                pageNum: 1,
                total: 0
            }
        },
        query: {
            title: undefined,
            type: undefined,
            content: undefined,
            code: undefined,
            ip: undefined
        },
        list: {
            type: [
                {
                    value: 'RR',
                    label: '自定义'
                },
                {
                    value: 'NullPointer',
                    label: '空指针'
                },
                {
                    value: 'Bind',
                    label: '校验'
                },
                {
                    value: 'Path',
                    label: '路径'
                },
                {
                    value: 'Method',
                    label: '请求方式'
                },
                {
                    value: 'Other',
                    label: '其他'
                }
            ],

            code: SText.code
        },
        dialog: {
            show: false,
            item: {} as any,
            title: '详情'
        },
        deldialog: {
            show: false,
            form: {
                type: 'type',
                value: {
                    type: undefined as any,
                    code: undefined as any
                }
            },
            title: '清空异常日志',
            loading: false,
            sub: async () => {
                conf.deldialog.loading = true
                const _value = conf.deldialog.form.value as any
                const _type = conf.deldialog.form.type
                const res = await ApiLogSystem.removeAll(_type, _value[_type])
                conf.deldialog.loading = false
                conf.deldialog.show = false
                if (res.status) getTableData()
            },
            open: () => {
                conf.deldialog.form = {
                    type: 'type',
                    value: {
                        type: conf.list.type[0].value,
                        code: conf.list.code[0].value
                    }
                }
                conf.deldialog.show = true
            }
        }
    })

    const getTableData = async () => {
        conf.table.loading = true
        const res = await ApiLogSystem.list({
            pageSize: conf.table.page.pageSize,
            pageNum: conf.table.page.pageNum,
            title: conf.query.title,
            type: conf.query.type,
            content: conf.query.content,
            code: conf.query.code,
            ip: conf.query.ip
        })
        if (res.status) {
            conf.table.data = res.data.map((_item: any) => {
                _item.createTime = new Date(_item.createTime).Format()
                return _item
            })
            conf.table.page.total = res.total
        }
        conf.table.loading = false
    }

    const resetQuery = () => {
        conf.query = {
            title: undefined,
            type: undefined,
            content: undefined,
            code: undefined,
            ip: undefined
        }
    }

    const showDetail = (item: any) => {
        conf.dialog.show = true
        conf.dialog.item = item
    }

    const search = () => {
        conf.table.page.pageNum = 1
        getTableData()
    }

    onMounted(async () => {
        await getTableData()
    })

    return { conf, getTableData, resetQuery, showDetail, search }
}
